package example.string;

/**
 * Created by lenovo on 2016/11/4 0004.
 * 字符串创建，不同方式的效率问题，看样子还是直接写字符串比较快
 */
public class Concatenate {
    public static void main(String[] args) {
        long startTime = System.nanoTime();
        for (int i = 0; i < 5000; i++) {
            String result = "This is"
                    + "testing the"
                    + "difference" + "between"
                    + "String" + "and" + "StringBuffer";
        }
        long endTime = System.nanoTime();
        System.out.println("字符串连接"
                + " - 使用 + 操作符 : "
                + (endTime - startTime) + " ns");
        long startTime1 = System.nanoTime();
        for (int i = 0; i < 5000; i++) {
            StringBuffer result = new StringBuffer();
            result.append("This is");
            result.append("testing the");
            result.append("difference");
            result.append("between");
            result.append("String");
            result.append("and");
            result.append("StringBuffer");
        }
        long endTime1 = System.nanoTime();
        System.out.println("字符串连接"
                + " - 使用 StringBuffer : "
                + (endTime1 - startTime1) + " ns");
        long startTime2 = System.nanoTime();
        for (int i = 0; i < 5000; i++) {
            StringBuilder result = new StringBuilder();
            result.append("This is");
            result.append("testing the");
            result.append("difference");
            result.append("between");
            result.append("String");
            result.append("and");
            result.append("StringBuffer");
        }
        long endTime2 = System.nanoTime();
        System.out.println("字符串连接"
                + " - 使用 StringBuilder : "
                + (endTime2 - startTime2) + " ns");
    }
}
